package client.gui.lobby;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import lib.Config;

public class mapSelector extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JComboBox jComboBox = null;
	private String[] maps = new String[] { "Maps" };
	private MapViewer viewer = null;
	private SelectorUser parent;

	/**
	 * This is the default constructor
	 */
	public mapSelector(SelectorUser parent) {
		super();
		this.parent = parent;
		load();
		float resize =0.3f;
		viewer = new MapViewer(new Dimension((int) (Config.xRes * resize), (int) (Config.yRes * resize)),resize);	
		initialize();
		//TODO get correct size :P
		this.setSize(new Dimension((int) (Config.xRes * resize+this.getLocation().getX()),(int) (Config.yRes * resize+this.getLocation().getY()+jComboBox.getHeight())));
		viewer.map = maps[0];
		viewer.repaint();
		
	}

	private void load() {
		File file = new File("maps/");
		String[] temp = file.list();
		int i = 0;
		for (String s : temp) {
			if (s.endsWith(".map"))
				i++;
		}
		maps = new String[i];
		i = 0;
		for (String s : temp) {
			if (s.endsWith(".map"))
				maps[i++] = s;
		}
		if (maps.length > 0)
			Config.mapName = maps[0];
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLocation(new Point(50, 50));
		this.setContentPane(getJContentPane());
		this.setTitle("MapSelectro");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				parent.setMap(Config.mapName);
			}
		});
		this.setAlwaysOnTop(true);	
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJComboBox(), BorderLayout.NORTH);
			jContentPane.add(viewer, BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox(maps);
			jComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					viewer.map = maps[jComboBox.getSelectedIndex()];
					Config.mapName = maps[jComboBox.getSelectedIndex()];
					//parent.setMap(maps[jComboBox.getSelectedIndex()]);
					viewer.repaint();
				}
			});
		}
		return jComboBox;
	}

} // @jve:decl-index=0:visual-constraint="47,86"
